package com.frxuexi.park2212.repair

import androidx.lifecycle.MutableLiveData
import com.frxuexi.lib_base.base.BaseViewModel
import com.frxuexi.lib_base.db.SearchDao
import com.frxuexi.lib_base.entity.RepairBean
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.entity.SearchBean
import com.frxuexi.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class RepairViewModel @Inject constructor(val apiService: ApiService,val searchDao: SearchDao):BaseViewModel() {
    var getRepir = MutableLiveData<RepairBean>()

    var getRepirByNo = MutableLiveData<RepairBeanItem>()

    var getRepirById = MutableLiveData<RepairBeanItem>()

    var updateRepire= MutableLiveData<RepairBeanItem>()

    var addRepire= MutableLiveData<RepairBeanItem>()

    var deleteRepire= MutableLiveData<String>()

    var inserSearch = MutableLiveData<Long>()
    var getSearchs = MutableLiveData<List<SearchBean>>()
    var deleteSearch = MutableLiveData<Int>()

    fun getRepairs(){
        httpRequest({apiService.getRepairs()},getRepir::postValue)
    }

    fun getRepairsByUname(name:String){
        httpRequest({apiService.getRepairsByUname(name)},getRepir::postValue)
    }

    fun getRepairByNo(no:String){
        httpRequest({apiService.getRepairByNo(no)},getRepirByNo::postValue)
    }

    fun getRepairById(id:Int){
        httpRequest({apiService.getRepairById(id)},getRepirById::postValue)
    }

    fun upadteRepair(id:Int,beanItem: RepairBeanItem){
        httpRequest({apiService.updateRepair(id,toBody(beanItem))},updateRepire::postValue)
    }

    fun addRepair(beanItem: RepairBeanItem){
        httpRequest({apiService.addRepair(toBody(beanItem))},addRepire::postValue)
    }

    fun deleteRepair(id:Int){
        httpRequest({apiService.deleteRepair(id)},deleteRepire::postValue)
    }

    fun insertSearch(bean: SearchBean){
        roomRequest({searchDao.addSearch(bean)},inserSearch::postValue)
    }

    fun getSearchs(){
        roomRequest({searchDao.getSearchs()},getSearchs::postValue)
    }

    fun deleteSearch(bean: SearchBean){
        roomRequest({searchDao.deleteSearch(bean)},deleteSearch::postValue)
    }
}